package org.leviatan.definator.core.admin;

import org.leviatan.common.keyutils.KeyGenerator;
import org.leviatan.definator.core.model.Field;
import org.leviatan.definator.core.model.impl.FieldImpl;

public class FieldHandler {
	
	private static Field activeFld = null;
	
	
	
	public static boolean updateFieldName(String fldId, String newName) {
		Field fld = RegistryHandler.getFieldModel(fldId);
		if (fld != null) {
			fld.setName(newName);
			return true;
		} else
			return false;
	}
	
	public static boolean updateFieldLength(String fldId, int newLength) {
		Field fld = RegistryHandler.getFieldModel(fldId);
		if (fld != null) {
			fld.setLength(newLength);
			return true;
		} else
			return false;
	}
	
	public static boolean updateFieldType(String fldId, String newType) {
		Field fld = RegistryHandler.getFieldModel(fldId);
		if (fld != null) {
			fld.setType(newType);
			return true;
		} else
			return false;
	}
	
	public static boolean updateFieldRecordRef(String fldId, String newRef) {
		Field fld = RegistryHandler.getFieldModel(fldId);
		if (fld != null) {
			fld.setRecordRef(newRef);
			return true;
		} else
			return false;
	}
	
	public static boolean updateField(String fldId, String newName, int newLength,
			String newType, String newRef) {
		boolean result = 
			(FieldHandler.updateFieldName(fldId, newName) && 
					FieldHandler.updateFieldLength(fldId, newLength) &&
					FieldHandler.updateFieldType(fldId, newType) &&
					FieldHandler.updateFieldRecordRef(fldId, newRef) );
		return result;
	}
	
	public static boolean updateField(Field updateInfo) {
		FieldHandler.activeFld.setName(updateInfo.getName());
		FieldHandler.activeFld.setLength(updateInfo.getLength());
		FieldHandler.activeFld.setRecordRef(updateInfo.getRecordRef());
		FieldHandler.activeFld.setType(updateInfo.getType());
		return true;
	}
	
	public static Field setFieldModel(String fldId) {
		Field fld = RegistryHandler.getFieldModel(fldId);
		return FieldHandler.setFieldModel(fld);
	}
	
	public static Field setFieldModel(Field aField) {
		FieldHandler.activeFld = aField;
		return FieldHandler.activeFld;
	}
	
	public static Field getActiveField() {
		return FieldHandler.activeFld;
	}
	
	/* Creates a new field with a new unique id */
	public static Field createNewField() {
		Field newFld = new FieldImpl();
		newFld.setId(KeyGenerator.generateUniqueKeyAsString());
		FieldHandler.activeFld = newFld;
		return newFld;
	}
	
	public static Field setFieldName(String aName) {
		FieldHandler.activeFld.setName(aName);
		return FieldHandler.activeFld;
	}
	
	public static Field setFieldLength(Integer aLength) {
		FieldHandler.activeFld.setLength(aLength);
		return FieldHandler.activeFld;
	}
	
	public static Field setFieldData(String someData) {
		FieldHandler.activeFld.setData(someData);
		return FieldHandler.activeFld;
	}
	
	public static Field setFieldType(String aType) {
		FieldHandler.activeFld.setType(aType);
		return FieldHandler.activeFld;
	}
	
	public static Field setFieldRecordRef(String aRef) {
		FieldHandler.activeFld.setRecordRef(aRef);
		return FieldHandler.activeFld;
	}
	
	public static String getFieldId() {
		return FieldHandler.activeFld.getId();
	}
	
	public static String getFieldName() {
		return FieldHandler.activeFld.getName();
	}
	
	public static Integer getFieldLength() {
		return FieldHandler.activeFld.getLength();
	}
	
	public static Object getFieldData() {
		return FieldHandler.activeFld.getData();
	}
	
	public static String getFieldType() {
		return FieldHandler.activeFld.getType();
	}
	
	public static String getFieldRecordRef() {
		return FieldHandler.activeFld.getRecordRef();
	}
	
}
